package copyengine.ui.component.button
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	import copyengine.ui.component.CEUISprite;
	import copyengine.ui.component.button.animation.ICEButtonAnimation;
	import copyengine.utils.GeneralUtils;

	/**
	 * CEButton is button component root.
	 * child class will deal with how the button changing it skin(ex:CEButtonFram , CEButtonTween)
	 * other class should only use CEButton , not it child class directly.
	 * ex:
	 * 		var button:CEButton = new CEButtonTween();
	 *
	 * @author Tunied
	 */
	public class CEButton extends CEUISprite
	{


		private var labelText:String;

		private var buttonBg:MovieClip;

		protected var onClickCallBackFunction:Function;

		protected var animation:ICEButtonAnimation;

		//标识当前是否已经MouseDown过,如果MouseDown且MouseUp后则相应Click事件
		private var isMarkMouseDown:Boolean;

		/**
		 * Create new CEButton
		 *
		 * @param _buttonBg									元件MC,用于传入Animation进行显示操作,CEButton本身不对该参数进行操作
		 *
		 * @param _labelText									需要绑定的文本,如果swf素材中当前元件含有<b>"labelTF"</b>字样,会将该文字写入该TF
		 *
		 * @param _onClickCallBackFunction  		点击事件的回调函数,Button默认不向上抛出Event,而是本身监听MouseDown和MouseUp事件,
		 * 																	当两个事件均响应到的时候则认为Click,回调该Callback
		 *
		 */
		public function CEButton(_buttonBg:MovieClip, _labelTextKey:String=null, _onClickCallBackFunction:Function=null)
		{
			super(_buttonBg);

			buttonBg=_buttonBg;
			labelText=_labelTextKey;
			onClickCallBackFunction=_onClickCallBackFunction;
			isMarkMouseDown=false;

			boundFont();
			addListener();
		}

		//==================================
		//==Initialize And Dispose Function
		//==================================

		override protected function doDispose():void
		{
			animation && animation.dispose();
			animation=null;
			removeListener();
		}


		/**
		 * 刷新按钮的Label签
		 *
		 * 有些逻辑,比如CellRender中每次都要刷新按钮的文本。ex A显示“张三” B显示“李四”
		 * 调用该函数可以重置按钮上的文本
		 *
		 */
		public function resetLabelText(_labelText:String):void
		{
			labelText=_labelText;
			boundFont();
		}

		public function setAnimation(_animation:ICEButtonAnimation):void
		{
			animation=_animation;
		}

		/**
		 * 可以在初始化时候设置Click回调，但是像Tabbar组件,是在内部包含N个按钮
		 * 此时无法在初始化时候设置按钮Callback事件,所以调用该函数设置
		 */
		public final function setOnClickCallBackFunction(_onClickCallBackFunction:Function):void
		{
			onClickCallBackFunction=_onClickCallBackFunction;
		}


		private function addListener():void
		{
			GeneralUtils.addTargetEventListener(buttonBg, MouseEvent.MOUSE_UP, onMouseUp);
			GeneralUtils.addTargetEventListener(buttonBg, MouseEvent.MOUSE_DOWN, onMouseDown);
			GeneralUtils.addTargetEventListener(buttonBg, MouseEvent.ROLL_OVER, onMouseRollOver);
			GeneralUtils.addTargetEventListener(buttonBg, MouseEvent.ROLL_OUT, onMouseRollOut);
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(buttonBg, MouseEvent.MOUSE_UP, onMouseUp);
			GeneralUtils.removeTargetEventListener(buttonBg, MouseEvent.MOUSE_DOWN, onMouseDown);
			GeneralUtils.removeTargetEventListener(buttonBg, MouseEvent.ROLL_OVER, onMouseRollOver);
			GeneralUtils.removeTargetEventListener(buttonBg, MouseEvent.ROLL_OUT, onMouseRollOut);
		}

		protected function onMouseRollOver(e:MouseEvent):void
		{
			animation && animation.onMouseRollOver(e);
			boundFont();
		}

		protected function onMouseRollOut(e:MouseEvent):void
		{
			animation && animation.onMouseRollOut(e);
			boundFont();
			isMarkMouseDown=false;
		}

		protected function onMouseUp(e:MouseEvent):void
		{
			animation && animation.onMouseUp(e);
			boundFont();
			if (isMarkMouseDown)
			{
				doOnClickButton();
				onClickCallBackFunction && onClickCallBackFunction(this);
			}
			isMarkMouseDown=false;
		}

		protected function onMouseDown(e:MouseEvent):void
		{
			isMarkMouseDown=true;
			animation && animation.onMouseDown(e);
			boundFont();
		}

		protected final function boundFont():void
		{
//			var lableField:TextField=buttonBg["label"];
//			if (lableField && labelText != null)
//			{
//				lableField.mouseEnabled=false;
//				FontEmbedManger.instance.embedTextField(lableField);
//				lableField.text=CETextManger.instance.getText(labelTextKey);
//			}
		}

		/**
		 * 如果子类需要在按钮点击后执行操作则重载该函数
		 */
		protected function doOnClickButton():void
		{

		}

	}
}
